//
//  LKChooseProductViewController.m
//  YLHelper
//
//  Created by she1323 on 2018/5/3.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKChooseProductViewController.h"
#import "LKWorkOrderListDetailCell.h"
#import "LKOrderEditBottomView.h"
#import "LKOrderEditTopView.h"
#import "LKChooseProductDetailViewController.h"
#import "LKAddProtocalMessageViewController.h"

@interface LKChooseProductViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)LKBaseTableview *tableview;
@property(nonatomic,strong)NSMutableArray *titlesArray;
@property(nonatomic,strong)LKOrderEditBottomView *bottomView;

@end

@implementation LKChooseProductViewController
-(LKBaseTableview *)tableview{
    if (!_tableview) {
        WS(weakSelf)
        _bottomView = [[[NSBundle mainBundle] loadNibNamed:@"LKOrderEditBottomView" owner:self options:nil] lastObject];
        [self.view addSubview:_bottomView];
        [[_bottomView.forwardBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }];
        [[_bottomView.nextBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            if (weakSelf.productModel.productName == nil) {
                [SVProgressHUD showInfoWithStatus:@"请选择产品"];
                return;
            }
            LKAddProtocalMessageViewController *vc = [[LKAddProtocalMessageViewController alloc] init];
            vc.customermodel = weakSelf.customerModel;
            weakSelf.addOrderModel.ProductName = weakSelf.productModel.productName;
            weakSelf.addOrderModel.ProductCode = weakSelf.productModel.productCode;
            weakSelf.addOrderModel.ProductCategory = weakSelf.productModel.productCategory;
            weakSelf.addOrderModel.LeastMoney = weakSelf.productModel.leastMoney;
            weakSelf.addOrderModel.IncreaseRadix = weakSelf.productModel.increaseRadixText;
            weakSelf.addOrderModel.ProductTerm = weakSelf.productModel.productTerm;
            weakSelf.addOrderModel.PayCategory = weakSelf.productModel.payCategory;
            weakSelf.addOrderModel.ProductAnnualizedYield = weakSelf.productModel.annualizedYield;
            weakSelf.addOrderModel.ProductCategoryS = weakSelf.productModel.productStateS;
            weakSelf.addOrderModel.ProductId = weakSelf.productModel.productId;
            vc.addOrderModel = weakSelf.addOrderModel;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }];
        [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.height.mas_equalTo(69);
        }];
        _tableview = [LKBaseTableview setTableviewWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT + 10, WIDTH , HEIGHT - NAVIGATION_BAR_HEIGHT - 10 - 70) style:UITableViewStyleGrouped registercellClasss:@[@"LKWorkOrderListDetailCell"]];
        [self.view addSubview:_tableview];
        _tableview.delegate = self;
        _tableview.bounces = NO;
        _tableview.dataSource = self;
        _tableview.backgroundColor = [UIColor whiteColor];
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableview;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.isUpateOrder) {
//        self.productModel = [[LKProductModel alloc] init];
        self.tableview.frame =CGRectMake(0, NAVIGATION_BAR_HEIGHT + 10, WIDTH , HEIGHT - NAVIGATION_BAR_HEIGHT - 10);
        [self.bottomView removeFromSuperview];
//        self.tableview.estimatedRowHeight = 200;
        [self.titlesArray removeAllObjects];
        NSString *productName = [NSString stringWithFormat:@"%@%@",@"产品名称：",self.productModel.productName];
        NSString *productCode = [NSString stringWithFormat:@"%@%@",@"产品编号：",self.productModel.productCode];
        NSString *productType = [NSString stringWithFormat:@"%@%@",@"产品类型：",[NSString backProductTypeAccordingStatusType:[self.productModel.productCategory integerValue]]];
        
        NSString *leastMoney = [NSString stringWithFormat:@"%@%@",@"起投金额：",self.productModel.leastMoney];
        
        NSString *dizengEdu = [NSString stringWithFormat:@"%@%@",@"递增额度：",self.productModel.productName];
        NSString *productTerm = [NSString stringWithFormat:@"%@%@",@"产品期限：",self.productModel.productTerm];
        NSString *productDuiHuan = [NSString stringWithFormat:@"%@%@",@"兑换方式：",[self accordingDuiFuFangshiType:[self.productModel.payCategory integerValue]]];
        NSString *nianhuashouyi = [NSString stringWithFormat:@"%@%@",@"年化收益：",self.productModel.annualizedYield];
        [self.titlesArray addObjectsFromArray:@[productName,productCode,productType,leastMoney,dizengEdu,productTerm,productDuiHuan,nianhuashouyi]];
        [self.tableview reloadData];
    }else{
       self.tableview.estimatedRowHeight = 200;
    }
    
    // Do any additional setup after loading the view.
}
-(void)setNavgationBackBtn{
    [super setNavgationBackBtn];
    self.navigationItem.title = @"选择产品";
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titlesArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LKWorkOrderListDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKWorkOrderListDetailCell"];
    cell.arrowBtn.hidden = YES;
    cell.lineView.hidden = YES;
    if (indexPath.row == 0) {
        cell.lineView.hidden = NO;
    }
    cell.titileLabel.text = self.titlesArray[indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LKOrderEditTopView *topView = [[[NSBundle mainBundle] loadNibNamed:@"LKOrderEditTopView" owner:self options:nil] lastObject];
    [topView.leftBtn setTitle:@" 产品信息" forState:UIControlStateNormal];
    [topView.leftBtn setImage:[UIImage imageNamed:@"productInfor"] forState:UIControlStateNormal];
    [topView.rightBtn setTitle:@" 选择" forState:UIControlStateNormal];
    [topView.rightBtn setImage:[UIImage imageNamed:@"productChoose"] forState:UIControlStateNormal];
    [topView.rightBtn addTarget:self action:@selector(chooseProductList:) forControlEvents:UIControlEventTouchUpInside];
    return topView;
}

/**
 选择产品列表添加

 */
-(void)chooseProductList:(UIButton *)btn{
    LKChooseProductDetailViewController *vc = [[LKChooseProductDetailViewController alloc] init];
    vc.IsAddOrder = YES;
    vc.customerModel = self.customerModel;
    WS(weakSelf)
    vc.productmodelBlock = ^(LKProductModel *model) {
        SS(strongSelf)
        [strongSelf.titlesArray removeAllObjects];
        strongSelf.productModel = model;
        NSString *productName = [NSString stringWithFormat:@"%@%@",@"产品名称：",strongSelf.productModel.productName];
        NSString *productCode = [NSString stringWithFormat:@"%@%@",@"产品编号：",strongSelf.productModel.productCode];
        NSString *productType = [NSString stringWithFormat:@"%@%@",@"产品类型：",[NSString backProductTypeAccordingStatusType:[strongSelf.productModel.productCategory integerValue]]];
        
        NSString *leastMoney = [NSString stringWithFormat:@"%@%@",@"起投金额：",strongSelf.productModel.leastMoney];
        
        NSString *dizengEdu = [NSString stringWithFormat:@"%@%@",@"递增额度：",strongSelf.productModel.increaseRadixText];
        NSString *productTerm = [NSString stringWithFormat:@"%@%@",@"产品期限：",strongSelf.productModel.productTerm];
        NSString *productDuiHuan = [NSString stringWithFormat:@"%@%@",@"兑换方式：",[strongSelf accordingDuiFuFangshiType:[strongSelf.productModel.payCategory integerValue]]];
        NSString *nianhuashouyi = [NSString stringWithFormat:@"%@%@",@"年化收益：",strongSelf.productModel.annualizedYield];
        [_titlesArray addObjectsFromArray:@[productName,productCode,productType,leastMoney,dizengEdu,productTerm,productDuiHuan,nianhuashouyi]];
        [strongSelf.tableview reloadData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 43;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] init];
    footerView.backgroundColor = [UIColor whiteColor];
    return footerView;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.000000000000001;
}

-(NSMutableArray *)titlesArray{
    if (!_titlesArray) {
        _titlesArray = [[NSMutableArray alloc] init];
        if (!self.productModel) {
//            [_titlesArray addObjectsFromArray:@[@"产品名称：",@"产品编号：",@"产品类型：",@"产品类型：",@"起投金额：",@"递增额度：",@"产品期限：",@"兑换方式：",@"年化收益："]];
             [_titlesArray addObjectsFromArray:@[@"",@"",@"",@"",@"",@"",@"",@"",@""]];
            return _titlesArray;
        }
        NSString *productName = [NSString stringWithFormat:@"%@%@",@"产品名称：",self.productModel.productName];
        NSString *productCode = [NSString stringWithFormat:@"%@%@",@"产品编号：",self.productModel.productCode];
    NSString *productType = [NSString stringWithFormat:@"%@%@",@"产品类型：",[NSString backProductTypeAccordingStatusType:[self.productModel.productCategory integerValue]]];
        
    NSString *leastMoney = [NSString stringWithFormat:@"%@%@",@"起投金额：",self.productModel.leastMoney];
        
    NSString *dizengEdu = [NSString stringWithFormat:@"%@%@",@"递增额度：",self.productModel.productName];
    NSString *productTerm = [NSString stringWithFormat:@"%@%@",@"产品期限：",self.productModel.productTerm];
    NSString *productDuiHuan = [NSString stringWithFormat:@"%@%@",@"兑换方式：",[self accordingDuiFuFangshiType:[self.productModel.payCategory integerValue]]];
    NSString *nianhuashouyi = [NSString stringWithFormat:@"%@%@",@"年化收益：",self.productModel.annualizedYield];
        [_titlesArray addObjectsFromArray:@[productName,productCode,productType,leastMoney,dizengEdu,productTerm,productDuiHuan,nianhuashouyi]];
    }
    return _titlesArray;
}
-(NSString *)accordingDuiFuFangshiType:(NSInteger)type{
    if (type == 0) {
        return @"到期分配";
    }else if(type == 1){
        return @"按月分配";
    }
    return @"半年分配";
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
